package com.liukai.personalbills.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.liukai.personalbills.entity.Account;
import com.liukai.personalbills.entity.AccountMessage;
import com.liukai.personalbills.entity.UserMessage;
import com.liukai.personalbills.service.AccountService;
import com.liukai.personalbills.util.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import javax.xml.soap.Text;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @GetMapping("/findAllAccount")
    public AccountMessage findAllAccount(@RequestParam String username){

        String code = TextUtil.CODE_NO;
        List<Account> accounts = accountService.findAllAccount(username);
        if(accounts.size() > 0 && accounts != null){
            code = TextUtil.CODE_OK;
        }
        AccountMessage accountMessage = new AccountMessage(code,accounts);

        System.out.println(accountMessage.toString());
//        List<Account> data = accountMessage.getData();
//        for (Account datum : data) {
//            System.out.println("日期:"+datum.getDate() + "====>" + datum.getDateId());
//        }

        return accountMessage;
    }

    @PostMapping("/addAccount")
    public UserMessage addAccount(@RequestParam String accountJson,@RequestParam String username){

        String result = TextUtil.ADD_ACCOUNT_FAIL;
        String code = TextUtil.CODE_NO;

        Gson gson = new Gson();
        Account account = gson.fromJson(accountJson,Account.class);
        int dateId = getDateId(account.getDate());
        account.setDateId(dateId);// 获取日期Id
        String timeId = getTimeId(dateId,account.getTime());
        account.setTimeId(timeId);
        System.out.println("controller========>"+account.toString());
        int index = accountService.addAccount(account,username);

        if (index == 1){
            result = TextUtil.ADD_ACCOUNT_SUCCESS;
        }
        UserMessage userMessage = new UserMessage(code,result);

        return userMessage;
    }

    @PutMapping("/updateAccount")
    private UserMessage updateAccount(@RequestParam String accountJson,@RequestParam String username){

        String result = TextUtil.ADD_ACCOUNT_FAIL;
        String code = TextUtil.CODE_NO;

        Gson gson = new Gson();
        Account account = gson.fromJson(accountJson,Account.class);
        int dateId = getDateId(account.getDate());
        account.setDateId(dateId);// 获取日期Id
        System.out.println("controller========>"+account.toString());
        int index = accountService.updateAccount(account,username);

        if (index == 1){
            result = TextUtil.UPDATE_ACCOUNT_SUCCESS;
        }
        UserMessage userMessage = new UserMessage(code,result);

        return userMessage;

    }

    /**
     * 获取日期Id
     * @param date
     * @return
     */
    private int getDateId(String date){

        String[] split = date.split("-");
        String dateString = "";
        for (String s : split) {
            dateString = dateString + s;
        }

        return Integer.valueOf(dateString);
    }

    /**
     * 获取时间Id
     * @param dateId
     * @param time
     * @return
     */
    private String getTimeId(int dateId,String time){

        String[] split = time.split(":");
        String timeId = dateId + "";
        for (String s : split) {
            timeId = timeId + s;
        }

        return timeId;
    }

}
